//
//  PersonInfoViewController.m
//  PropertyManager
//
//  Created by TNG on 2018/8/13.
//  Copyright © 2018年 Tng. All rights reserved.
//

#import "PersonInfoViewController.h"

@interface PersonInfoViewController () <UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UITableView * tableview;
@property (strong, nonatomic) UIImage * userImg;
@property (nonatomic,copy) NSString * userName;

@end

@implementation PersonInfoViewController

static NSString * PersonSetHeadImgTableViewCellID = @"PersonSetHeadImgTableViewCell";
static NSString * PersonSetNameTableViewCellID = @"PersonSetNameTableViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"个人资料";
    
    [self creatTableview];
    [self registCell];
}

-(void)registCell
{
    [self.tableview registerClass:[PersonSetHeadImgTableViewCell class] forCellReuseIdentifier:PersonSetHeadImgTableViewCellID];
    [self.tableview registerClass:[PersonSetNameTableViewCell class] forCellReuseIdentifier:PersonSetNameTableViewCellID];
}

-(void)creatTableview{
    if (self.tableview == nil) {
        UITableView * tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, kStatusAndNavBarH, WIDTH, HEIGHT - kStatusAndNavBarH - kHomeLineH) style:UITableViewStyleGrouped];
        tableview.delegate = self;
        tableview.dataSource = self;
        if (@available(iOS 11.0, *)) {
            tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
        }
        self.tableview = tableview;
        [self.view addSubview:self.tableview];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0)
    {
        PersonSetHeadImgTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:PersonSetHeadImgTableViewCellID];
        if(self.userImg != nil)
        {
            cell.img.image = self.userImg;
        }else
        {
            [cell.img sd_setImageWithURL:SFMheadimgurl completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                cell.img.image = image;
            }];
        }
        return cell;
    }else
    {
        PersonSetNameTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:PersonSetNameTableViewCellID];
        cell.detailLabel.text = self.userName?self.userName:SFMloginname;
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WeakSelf;
    if(indexPath.row == 0)
    {
        PersonChangImgViewController * ctr = [[PersonChangImgViewController alloc] init];
        ctr.personChangImgBlock = ^(UIImage *img) {
            weakSelf.userImg = img;
            [weakSelf.tableview reloadData];
        };
        [self.navigationController pushViewController:ctr animated:YES];
    }else
    {
        PersonChangeNameViewController * ctr = [[PersonChangeNameViewController alloc] init];
        ctr.name = self.userName?self.userName:SFMloginname;
        ctr.personChangeNameEventBlock = ^(NSString *name) {
            weakSelf.userName = name;
            [weakSelf.tableview reloadData];
        };
        [self.navigationController pushViewController:ctr animated:YES];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
